How many types of Data Types Numpy Have In Python?

How many types of Data Types Numpy Have In Python?




By default Python have these below data types:

strings - Strings are used to represent text data, the text is given under quote marks. e.g. "DERF"

integer - Integers are used to represent integer numbers. e.g. -20, +43, -7

float - Floats are used to represent real numbers with the decimal values. e.g. 1.2, 42.42

boolean - Booleans are used to represent True or False values.

complex - Complexs are used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j


NumPy also has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

i - integer

b - boolean

u - unsigned integer

f - float

c - complex float

m - timedelta

M - datetime

O - object

S - string

U - unicode string

V - fixed chunk of memory for other type ( void )


How To Check The Data Type Of An Array?

You can find the data type of an array with property called dtype

Let's see in the below example to see how we cann find the data type of the below array arr

import numpy as np

arr = np.array([78, 56, 97, 89])

print(arr.dtype)

Output:

Int64

How To get The Data Type Of An Array In Numpy Which Contains Strings?

import numpy as np

arr = np.array(['apple', 'banana', 'cherry'])

Output:

<U6

How To Create Array In Numpy With Defined Data Types?

You can use the array() function to create arrays,this function can take an optional argument: dtype that will allow you to create a desired data typesas per your choice for the array

Let's see in the below example how we can create String data type array

Example

import numpy as np

arr = np.array([1, 2, 3, 4], dtype='S')

print(arr)

print(arr.dtype)

Output:

[b'1' b'2' b'3' b'4']

|S1

How To Create Numpy Array with Data Type as Integer?

In the below example you can see how array can be created with 4 byte Integer Data Type

import numpy as np

arr = np.array([32, 54, 67, 96], dtype='i4')

print(arr)

print(arr.dtype)

Output:

[32 54 67 96]

int32

What will happen if a value can not be converted by by dtype for Array?

If a type has been given by dtype element for the array but it is not possible to cast to the data type then numpy will throw the below error

ValueError: In Python ValueError is raised when the type of passed argument to a function is unexpected/incorrect.

Example:

Let's work with the below example to convert a string like 'b' to an integer value 

import numpy as np

arr = np.array(['b', '7', '9'], dtype='i')

Output:

Traceback (most recent call last):

  File "./prog.py", line 3, in 

ValueError: invalid literal for int() with base 10: 'b'

As here we are trying to convert the string 'b' to an integer value hence numpy throws the above error

How To Convert The Data Type Of An Existing Array In Numpy?

To convert the data type of an existing array you can first copy the array with the astype() method

The astype() function creates a copy of the array, and allows you to specify the data type as a parameter.

The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

In the below example we are changing the float values of the array to integer with the astype function 

Change data type from float to integer by using 'i' as parameter value:

import numpy as np

arr = np.array([4.5, 8.9, 3.8])

newarr = arr.astype('i')

print(newarr)

print(newarr.dtype)

Output:

[4 8 3]

int32

Explanation:

With the astype function we are converting the float values to integer values a and keeping the array values in the new array newarr

With the two ouput values we are actaully printing the new array value and the data type of the new array

You can perform the same operation by using int as the parameter value like astype(int)

You can convert a value to boolean by using the same astype function and by passing bool as the parameter to the function



Comments